package com.chidt.zpro.controller;

import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.crypto.SecureUtil;
import com.chidt.zpro.entity.User;
import com.chidt.zpro.service.UserService;
import com.chidt.zpro.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;

@WebServlet("/user/register")
public class UserRegisterController extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String confirmPassword = req.getParameter("confirm-password");


        if (username.isBlank()|| password.isBlank() || email.isBlank() || confirmPassword.isBlank()) {
            req.setAttribute("msg", "参数不能为空");
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
            return;
        }
        if (password.length() < 6) {
            req.setAttribute("msg", "密码长度不能小于6位");
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
            return;
        }

        if (!password.equals(confirmPassword)) {
            req.setAttribute("msg", "两次密码不一致");
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
            return;
        }

        // 邮箱正则表达式
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        if (!email.matches(emailRegex)) {
            req.setAttribute("msg", "邮箱格式不正确");
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
            return;
        }
        System.out.println("注册合法性校验通过...");
        UserService userService = new UserServiceImpl();
        User user = new User();
        user.setUsername(username);
        String md5 = SecureUtil.md5(password);
        user.setPassword(md5);
        user.setEmail(email);
        try {
            // 调用service层注册方法
            userService.register(user);
            System.out.println("注册成功,跳转至登录页面...");
            /*重定向*/
            String encode = URLEncodeUtil.encode("注册成功，请登录");
//            resp.sendRedirect("/zpro/login.jsp"+"?msg="+encode);
//            resp.sendRedirect(req.getContextPath() + "/login.jsp"+"?msg="+encode);//重定向
//            req.setAttribute("msg", encode);

            /*转发*/
//            req.getRequestDispatcher("/login.jsp").forward(req, resp);//转发,页面样式会乱

            /*值放session里*/
            req.getSession().setAttribute("msg","注册成功，请登录");
            resp.sendRedirect(req.getContextPath() + "/login.jsp");//重定向
        } catch (SQLException e) {
//            System.out.println("注册失败，数据库异常");
            goRegisterPage(req, resp,"用户或邮箱已存在");
        } catch (IOException e) {
            goRegisterPage(req, resp,"服务器繁忙，请稍后再试");
        }

    }


    public static void goRegisterPage(HttpServletRequest req, HttpServletResponse resp,String msg) throws ServletException, IOException {
        req.setAttribute("msg", msg);
        req.getRequestDispatcher("/register.jsp").forward(req, resp);
    }
    public static void main(String[] args) {
//        System.out.println("注册成功,跳转至登录页面...");
    }
}
